<?php
class ControllerNewsCategory extends Controller {
	private $error = array();

	public function index() {
		$this->language->load('news/category');
	
		$this->data['breadcrumbs'] = array();
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),
			'separator' => false
		);

		$category_id = isset($this->request->get['category_id']) ? $this->request->get['category_id'] : 0;
		
		$limit = isset($this->request->get['limit']) ? $this->request->get['limit'] : 5;
		$page = isset($this->request->get['page']) ? $this->request->get['page'] : 1;
		$data = array(
			'filter_category_id'	=> $category_id,
			'start' 		 		=> ($page - 1) * $limit,
			'limit' 		 		=> $limit
		);
		
		$this->load->model('news/news');
		$article_list = $this->model_news_news->getNews($data);
				
		if ($article_list) {		
			$this->data['limit'] = $limit;
			
			$url =  'category_id=' .$category_id;
			
			$this->data['limits'] = array();				
			$this->data['limits'][] = array(
				'text'  => 5,
				'value' => 5,
				'href'  => $this->url->link('news/category', $url . '&limit=5')
			);
				
			$this->data['limits'][] = array(
				'text'  => 10,
				'value' => 10,
				'href'  => $this->url->link('news/category', $url . '&limit=10')
			);
	
			$this->data['limits'][] = array(
				'text'  => 15,
				'value' => 15,
				'href'  => $this->url->link('news/category', $url . '&limit=15')
			);
				
			$this->data['limits'][] = array(
				'text'  => 20,
				'value' => 20,
				'href'  => $this->url->link('news/category', $url . '&limit=20')
			);
			
			$pagination = new Pagination();
			$pagination->total = $this->model_news_news->getTotalNews($data);
			$pagination->page = $page;
			$pagination->limit = $limit;
			$pagination->text = $this->language->get('text_pagination');
			$pagination->url = $this->url->link('news/category', $url . '&limit=' . $limit . '&page={page}');
				
			$this->data['pagination'] = $pagination->render();
			
			$this->load->model('news/type');
			$type_id = $article_list[0]['type_id'];
			$news_type = $this->model_news_type->getNewsType($type_id); 
			
			$this->data['breadcrumbs'][] = array(
				'text'      => $news_type['name'],
				'href'      => $this->url->link('news/type', '&type_id=' . $type_id),
				'separator' => $this->language->get('text_separator')
			);
			
			$this->load->model('news/category');
			$category = $this->model_news_category->getCategory($category_id);
			$this->data['category_info'] =  $category;
			$this->data['breadcrumbs'][] = array(
				'text'      => $category['name'],
				'href'      => '#',
				'separator' => $this->language->get('text_separator')
			);
			
			$this->load->model('tool/image');
			foreach ($article_list as $article_info){
				if (!empty($article_info['image']) && file_exists(DIR_IMAGE .$article_info['image'])) {
					$thumb = $this->model_tool_image->resize($article_info['image'], 150, 150);
				} else {
					$thumb = $this->model_tool_image->resize('no_image.jpg', 150, 150);
				}
				
				$this->data['article_list'][] = array(
					'article_id'	=> $article_info['article_id'],
					'category_id' 	=> $article_info['category_id'],
					'image'			=> $thumb,
					'date_added'	=> $article_info['date_added'],
					'title'			=> $article_info['title'],
					'header'		=> $article_info['header'],
					'href' 			=> $this->url->link('news/news', 'article_id=' .$article_info['article_id'])
				);					
			}
						
			$this->document->addLink($this->url->link('news/category', 'category_id=' . $category_id), 'canonical');
			
			// Mapping all language variables
			$this->data = array_merge($this->data, $this->language->getData());
			
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/news/category.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/news/category.tpl';
			} else {
				$this->template = 'default/template/news/category.tpl';
			}

			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);
				
			$this->response->setOutput($this->render());
		} else {
			$url = '';

			$this->data['breadcrumbs'][] = array(
        		'text'      => $this->language->get('text_error'),
				'href'      => '#',
        		'separator' => $this->language->get('text_separator')
			);

			$this->document->setTitle($this->language->get('text_error'));

			$this->data['heading_title'] = $this->language->get('text_error');

			$this->data['text_error'] = $this->language->get('text_error');

			$this->data['button_continue'] = $this->language->get('button_continue');

			$this->data['continue'] = $this->url->link('common/home');

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$this->template = 'default/template/error/not_found.tpl';
			}

			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
			);
				
			$this->response->setOutput($this->render());
		}
	}
}
?>